package com.lc.tgxm.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lc.tgxm.BaseApplication;
import com.lc.tgxm.R;
import com.lc.tgxm.conn.RegAsyPost;
import com.lc.tgxm.conn.SmsAsyPost;
import com.lc.tgxm.model.User;
import com.lc.tgxm.util.CountDownTimerService;
import com.lc.tgxm.util.Log;
import com.lc.tgxm.util.Utils;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * RegActivity
 * Created by Lei on 2017/4/25.
 */
public class RegActivity extends BaseActivity {
    private TextView tvGetVcode;
    private EditText etName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenViewAndBack(R.layout.activity_reg, R.string.reg);

        etName = (EditText) findViewById(R.id.et_name);
        tvGetVcode = (TextView) findViewById(R.id.tv_vcode);

    }

    @Override
    public void onButtonClick(View view) {
        switch (view.getId()) {
            case R.id.btn_reg:// 注册
                regAction();
                break;
            case R.id.tv_vcode:// 获取验证码
                vcodeAction();
                break;
        }
    }

    // 获取验证码动作
    private void vcodeAction() {
        String name = etName.getText().toString();
        if (Utils.checkPhonenumber(name)) {
            // 走获取验证码接口
            smsAsyPost.phone = name;
            smsAsyPost.execute(context);
        }
    }

    // 验证码接口
    private SmsAsyPost smsAsyPost = new SmsAsyPost(new AsyCallBack<String>() {
        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            Log.i(TAG, "s", s);
            CountDownTimerService.getInstance().init(context, tvGetVcode).startTimer();
            UtilToast.show(toast);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    // 注册动作
    private void regAction() {
        EditText etVcode = (EditText) findViewById(R.id.et_vcode);
        EditText etpwd = (EditText) findViewById(R.id.et_pwd);
        EditText etpwd2 = (EditText) findViewById(R.id.et_pwd2);
        String name = etName.getText().toString();
        String vcodeStr = etVcode.getText().toString();
        String pwdstr1 = etpwd.getText().toString();
        String pwdstr2 = etpwd2.getText().toString();

        // 密码最小长度
        int pwdminLength = getResources().getInteger(R.integer.password_minlength);
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(vcodeStr) || TextUtils.isEmpty(pwdstr1)
                || TextUtils.isEmpty(pwdstr2)) {
            UtilToast.show(R.string.to_notnull);
        } else if (!Utils.checkMobile(name)) {// 手机号合法性
            UtilToast.show(R.string.to_phoneillegal);
        } else if (pwdstr1.length() < pwdminLength || pwdstr2.length() < pwdminLength) {// 密码长度
            UtilToast.show(String.format(getString(R.string.to_notminlength), pwdminLength));
        } else if (!pwdstr1.equals(pwdstr2)) {// 密码匹配
            UtilToast.show(R.string.to_notequal);
        } else {
            // 走接口
            Log.i(TAG, "Do Register conn interface", ":" + name + ":" + vcodeStr + ":" + pwdstr1 + ":" + pwdstr2);
            regAsyPost.phone = name;
            regAsyPost.code = vcodeStr;
            regAsyPost.user_pass = pwdstr1;
            regAsyPost.execute(context);
        }
    }

    // 注册接口
    private RegAsyPost regAsyPost = new RegAsyPost(new AsyCallBack<User>() {
        @Override
        public void onSuccess(String toast, int type, User user) throws Exception {
            UtilToast.show(toast);
            BaseApplication.basePreferences.putUserId(user.uid);
            startVerifyActivity(MainNavigationActivity.class);

            // noinspection unchecked
            BaseApplication.INSTANCE.finishActivity(RegActivity.class, LoginActivity.class);
            finish();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });

    @Override
    protected void onDestroy() {
        CountDownTimerService.getInstance().cancelTimer();
        super.onDestroy();
    }

}